package com.blog.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName HelloServlet
 * @Description TODO
 * @Author zhongge
 * @Version 1.0
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置响应内容类型
        /**
         * setContentType 设置文本类型（该方法在resp中才有）
         */
        resp.setContentType("text/html;charset=UTF-8");

        // 获取输出流 resp对象中有输出流对象==》用于将内容输出到浏览器
        PrintWriter out = resp.getWriter();

        // 输出HTML内容
        out.println("<html>");
        out.println("<head><title>JavaWeb入门</title></head>");
        out.println("<body>");
        out.println("<h1>🎉 恭喜！你的第一个Servlet程序运行成功！</h1>");
        out.println("<p>当前时间：" + new java.util.Date() + "</p>");
        out.println("</body></html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
